package com.ruoyi.tuser.service.impl;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.tuser.mapper.TUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.tuser.domain.TUser;
import com.ruoyi.tuser.service.ITUserService;

/**
 * 用户Service业务层处理
 *
 * @author ruoyi
 * @date 2024-11-08
 */
@Service
public class TUserServiceImpl implements ITUserService
{
    @Autowired
    private TUserMapper tUserMapper;

    /**
     * 查询用户
     *
     * @param userId 用户主键
     * @return 用户
     */
    @Override
    public TUser selectTUserByUserId(Long userId)
    {
        return tUserMapper.selectTUserByUserId(userId);
    }

    /**
     * 查询用户列表
     *
     * @param tUser 用户
     * @return 用户
     */
    @Override
    public List<TUser> selectTUserList(TUser tUser)
    {
        return tUserMapper.selectTUserList(tUser);
    }

    /**
     * 新增用户
     *
     * @param tUser 用户
     * @return 结果
     */
    @Override
    public int insertTUser(TUser tUser)
    {
        tUser.setCreateTime(DateUtils.getNowDate());

        // 使用MD5加密密码
        String encryptedPassword = md5(tUser.getUserPassword());
        tUser.setUserPassword(encryptedPassword);

        return tUserMapper.insertTUser(tUser);
    }

    /**
     * 修改用户
     *
     * @param tUser 用户
     * @return 结果
     */
    @Override
    public int updateTUser(TUser tUser)
    {
        tUser.setUpdateTime(DateUtils.getNowDate());
        String encryptedPassword = md5(tUser.getUserPassword());
        tUser.setUserPassword(encryptedPassword);
        return tUserMapper.updateTUser(tUser);
    }

    /**
     * 批量删除用户
     *
     * @param userIds 需要删除的用户主键
     * @return 结果
     */
    @Override
    public int deleteTUserByUserIds(Long[] userIds)
    {
        return tUserMapper.deleteTUserByUserIds(userIds);
    }

    /**
     * 删除用户信息
     *
     * @param userId 用户主键
     * @return 结果
     */
    @Override
    public int deleteTUserByUserId(Long userId)
    {
        return tUserMapper.deleteTUserByUserId(userId);
    }

    /**
     * 重置用户密码
     * @param tUser
     * @return
     */
    @Override
    public int updatePassword(TUser tUser) {
        String encryptedPassword = md5("123456");
        tUser.setUserPassword(encryptedPassword);
        return tUserMapper.updatePassword(tUser);
    }

    /**
     * 启用禁用
     * @param tUser
     * @return
     */
    @Override
    public int updateUserInfo(TUser tUser) {
        return tUserMapper.updateUserInfo(tUser);
    }

    @Override
    public List<TUser> findUserListByIds(Long[] userIds) {
        return tUserMapper.findUserListByIds(userIds);
    }

    // MD5 加密方法
    private String md5(String password) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] messageDigest = md.digest(password.getBytes());
            StringBuilder sb = new StringBuilder();
            for (byte b : messageDigest) {
                sb.append(String.format("%02x", b)); // 转换为十六进制字符串
            }
            return sb.toString();
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e); // 处理异常
        }
    }
}
